package cn.jsprun.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class ShowFileSize extends TagSupport {
	private static final long serialVersionUID = -8940560586441587185L;
	String size;
	
	public void setSize(String size){
		this.size = size;
	}
	
	
	@Override
	public int doStartTag() throws JspException {
		long fileSize = Long.parseLong(size);
		double size = 0;
		java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
		try {
			if(fileSize<1024){
				pageContext.getOut().write(size +" Bytes");
			}else if(fileSize>1024 && fileSize<1048576){
				size = (double)fileSize/1024;
				pageContext.getOut().write(df.format(size)+" KB");
			}else if(fileSize>1048576 && fileSize<1073741824){
				size = (double)fileSize/1048576;
				pageContext.getOut().write(df.format(size)+" MB");
			}else{
				size = (double)fileSize/1073741824;
				pageContext.getOut().write(df.format(size)+" GB");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	

}
